package com.example.controller;

import com.example.annotation.PassToken;
import com.github.binarywang.wxpay.bean.order.WxPayNativeOrderResult;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author ChenXiaoBin
 * on 2019-09-23
 */
@RestController
@RequestMapping("/V1.0/pay")
public class WeixinpayController {
    @Autowired
    private WxPayService wxService;
//
//    @Autowired
//    public WeixinpayController(WxPayService wxService) {
//        this.wxService = wxService;
//    }


    @RequestMapping("/pay_notify")
    @PassToken
    public void payNotify(HttpServletRequest request, HttpServletResponse response) {

        Integer i = 0;

        String f = "123321";
    }

    @GetMapping("/")
    @PassToken
    @ApiOperation(value = "获取微信支付二维码", notes = "获取微信支付二维码")
    public String payUrl() {
        WxPayUnifiedOrderRequest wxPayUnifiedOrderRequest = new WxPayUnifiedOrderRequest();
        wxPayUnifiedOrderRequest.setBody("红包充值");
        wxPayUnifiedOrderRequest.setOutTradeNo("122121221");
        wxPayUnifiedOrderRequest.setTotalFee(1);
        wxPayUnifiedOrderRequest.setSpbillCreateIp("127.0.0.1");
        wxPayUnifiedOrderRequest.setTradeType("NATIVE");
        wxPayUnifiedOrderRequest.setProductId("11111111111111111111111111111111");
        //回调地址
        wxPayUnifiedOrderRequest.setNotifyUrl("cxb.free.idcfengye.com/V1.0/pay/pay_notify");
        try {
            WxPayNativeOrderResult nativeOrderResult = wxService.createOrder(wxPayUnifiedOrderRequest);
            return nativeOrderResult.getCodeUrl();
        } catch (WxPayException e) {
            e.printStackTrace();
        }
        return null;
    }
}
